<?php

namespace App\Services;

use App\Exceptions\ValidationException;
use App\Models\VolunteerForm;
use App\Repositories\Contracts\VolunteerActivityRepository;
use App\Repositories\Contracts\VolunteerFormRepository;
use App\Repositories\Transformers\VolunteerFormTransformer;

class VolunteerFormService
{
    private $volunteerFormRepository;
    private $volunteerActivityRepository;

    public function __construct(VolunteerFormRepository $volunteerFormRepository, VolunteerActivityRepository $volunteerActivityRepository)
    {
        $this->volunteerFormRepository = $volunteerFormRepository;
        $this->volunteerActivityRepository = $volunteerActivityRepository;
    }

    /**
     * 获取列表
     *
     * @param array|null $param
     */
    public function getList($param = null)
    {
        try {
            $filter['add_uid'] = $param['add_uid'];

            $volunteerForm = $this->volunteerFormRepository->paginate($param['per_page'] ?? null, $filter);

            $formCount = VolunteerForm::select(\DB::raw('count(*) as volunteer_form_count, volunteer_id'))
                ->groupBy('volunteer_id')
                ->get();
            $countArr = $formCount->pluck('volunteer_form_count', 'volunteer_id')->toArray();

            foreach ($volunteerForm as &$value) {
                foreach ($countArr as $k => $v) {
                    if ($value->volunteer_id == $k) {
                        $value->volunteer_form_count = $v;
                    }
                }
            }
            $result = fractal($volunteerForm, new VolunteerFormTransformer());

            return $result->toArray();

        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 获取详情
     *
     * @param int $id
     * @return object|array
     */
    public function getInfo($id)
    {
        try {
            $result = $this->volunteerFormRepository->find($id);
            return $result;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 新增
     *
     * @param array $param
     */
    public function add($param)
    {

        // 先判断是否已提交过
        $where = [
            'volunteer_id' => $param['volunteer_id'],
            'add_uid' => $param['add_uid'],
        ];

        $form = $this->volunteerFormRepository->findByFilter($where);

        if ($form) {
            throw new ValidationException('已提交过');
        }

        $volunteerForm = $this->volunteerFormRepository->store($param);
        return $volunteerForm;

    }

    /**
     * 修改
     *
     * @param array $param
     * @param int $id
     */
    public function update($param, $id)
    {
        try {
            $volunteerForm = $this->volunteerFormRepository->update($param, $id);
            return $volunteerForm;
        } catch (\Throwable $th) {
            throw $th;
        }
    }

    /**
     * 删除
     *
     * @param int $id
     */
    public function delete($id)
    {
        try {
            $volunteerForm = $this->volunteerFormRepository->delete($id);

            $result = [];
            if ($volunteerForm) {
                $result = 'success';
            } else {
                $result = 'error';
            }
            return $result;

        } catch (\Throwable $th) {
            throw $th;
        }
    }
}
